package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.itheima.common.utils.AesUtils;
import com.itheima.common.vo.Result;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Date;

@Service(version = "1.0",interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Result send4Login(String telephone) {
        /**
         * 1. 发验证码 肯定要有验证码
         * 2. 给用户发短信 telephone 短信平台
         * 3. 验证码存在redis里面，设置一个过期时间，短信验证码的过期时间
         */
        int code = 123456;
//        int code = RandomUtils.nextInt(100000, 999999);
        System.out.println("发短信手机号:"+telephone+",验证码:"+code);
        //线程
//        boolean isSend = sendSms(telephone,code);
        boolean isSend = true;
        if (isSend){
            Jedis resource = jedisPool.getResource();
            resource.setex("LOGIN_SEND_"+telephone,300,String.valueOf(code));
            if (resource != null) {
                resource.close();
            }
        }
        return Result.success(null);
    }

    @Override
    public Result check(String telephone, String validateCode) {
        /**
         * 1. 去看验证码是否合法
         * 2. 手机号是否存在 ，会员表中，如果存在 登录 不存在 注册
         * 3. 怎么才算登录？ session ，服务 部署了两台机器 A，B ，给前端返回一个标识，前端在请求的时候携带此标识
         * 拿上这个标识 去redis做验证，存在就登录 不存在就未登录
         * 4. 给前端返回标识 （token），写入cookie
         * 5. token放入redis value 会员信息
         */
        Jedis resource = jedisPool.getResource();
        String redisCode = resource.get("LOGIN_SEND_" + telephone);
        if (redisCode == null || !redisCode.equals(validateCode)){
            return Result.fail(-999,"验证码错误");
        }
        Member memberByPhone = this.memberMapper.findMemberByPhone(telephone);
        if (memberByPhone == null){
            memberByPhone = new Member();
            memberByPhone.setRegTime(new Date());
            memberByPhone.setPhoneNumber(telephone);
            this.memberMapper.save(memberByPhone);
        }
        long currentTimeMillis = System.currentTimeMillis() + 24 * 3600 * 1000;
        //对称加密方式
        String token = AesUtils.encrypt(telephone + "_" + currentTimeMillis, "itheima@#$W");

        resource.setex("TOKEN_"+token, 24 * 3600,JSON.toJSONString(memberByPhone));
        resource.close();
        return Result.success(token);
    }

    @Override
    public Result userInfo(String token) {
        String decrypt = AesUtils.decrypt(token, "itheima@#$W");
        String[] split = decrypt.split("_");
        String time = split[1];
        long parseLong = Long.parseLong(time);
        if (parseLong <= System.currentTimeMillis()){
            return Result.fail(-999,"token过期");
        }
        Jedis resource = jedisPool.getResource();
        String memberJson = resource.get("TOKEN_" + token);
        if (memberJson == null){
            return Result.fail(-999,"token过期");
        }

        return Result.success(JSON.parseObject(memberJson,Member.class));
    }
}
